package com.example.origin.spider.webmagic;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.proxy.Proxy;
import us.codecraft.webmagic.proxy.SimpleProxyProvider;

import java.util.ArrayList;
import java.util.List;

public class ReturnProxyProvider extends SimpleProxyProvider {
    private final List<Proxy> proxies;

    public ReturnProxyProvider(List<Proxy> proxies) {
        super(proxies);
        this.proxies = proxies;
    }

    public ReturnProxyProvider() {
        this(new ArrayList<>());
    }

    public void addProxy(Proxy e) {
        this.proxies.add(e);
    }

    @Override
    public void returnProxy(Proxy proxy, Page page, Task task) {
        super.returnProxy(proxy, page, task);
        if (page.getStatusCode() != 200) {
            proxies.remove(proxy);
        }
    }
}
